﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Ninject;
using WP7DSMClient.Services.Resources;

namespace WP7DSMClient.Services.RunTime
{
    /// <summary>
    /// Provides storage facilities for settings.
    /// </summary>
    public class StorageSettingsService : IStorageSettingsService
    {
        #region Private members

        private IsolatedStorageSettings _isolatedStore;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes the storage settings accessor.
        /// </summary>
        public StorageSettingsService()
        {
            _isolatedStore = IsolatedStorageSettings.ApplicationSettings;
        }

        #endregion
        #region ISettingsStorageService Members

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="key">The key to identify the value to store.</param>
        /// <param name="value">The value to store.</param>
        /// <returns><b>true</b> if successfull, <b>false</b> otherwise.</returns>
        public bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            try
            {
                // if new value is different, set the new value.
                if (_isolatedStore[key] != value)
                {
                    _isolatedStore[key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                _isolatedStore.Add(key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                _isolatedStore.Add(key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                ILoggerService logger = StandardKernelService.Current.Get<ILoggerService>();
                logger.SendLog(Localization.IsolatedStorageAddError,e.ToString());                
            }

            return valueChanged;
        }


        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType">The type of the object to retrieve.</typeparam>
        /// <param name="key">The key of the object to retrieve.</param>
        /// <param name="defaultValue">The default value if the key doesn't exist.</param>
        /// <returns>The value retrieved.</returns>
        public valueType GetValueOrDefault<valueType>(string key, valueType defaultValue)
        {
            valueType value;
            
            try
            {
                if (!_isolatedStore.TryGetValue(key, out value))
                {
                    value = defaultValue;
                } 
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }

            return value;
        }


        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            _isolatedStore.Save();
        }

        #endregion
    }
}